﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;

namespace Boom.Navigation
{
    public class FileMap
    {
        private int[,] _Matrix;
        private StreamReader _MyStream;
        private string _FileName;

        //Các tọa độ đọc được trong map
        private Vector2 PositionPlayer;
        private List<Vector2> Position_DesBricks;
        private List<int> Type_DesBricks;
        private List<Vector2> Position_NonDesBricks;
        private List<int> Type_NonDesBricks;
        private List<Vector2> Position_Monsterlv1;
        private List<Vector2> Position_Monsterlv2;       
        private Vector2 PositionBoss;
        private bool _Boss;

        public int[,] Matrix
        {
            get { return _Matrix; }
            set { _Matrix = value; }
        }
        public string FileName
        {
            get { return _FileName; }
            set { _FileName = value; }
        }
        public List<Vector2> PosDesBricks
        {
            get { return Position_DesBricks; }
        }
        public List<int> TypeDesBricks
        {
            get { return Type_DesBricks; }
        }
        public List<Vector2> PosNonBricks
        {
            get { return Position_NonDesBricks; }
        }
        public List<int> TypeNonDesBricks
        {
            get { return Type_NonDesBricks; }
        }
        public List<Vector2> PosMonsterLv1
        {
            get { return Position_Monsterlv1; }
        }
        public List<Vector2> PosMonsterLv2
        {
            get { return Position_Monsterlv2; }
        }
        public Vector2 PosPlayer
        {
            get { return PositionPlayer; }
        }
        public Vector2 PosBoss
        {
            get { return PositionBoss; }
        }
        public bool Boss
        {
            get { return _Boss; }
        }

        public FileMap(string FileName)
        {
            this.FileName = FileName;
            _Matrix = new int[15, 15];
            _MyStream = new StreamReader(this.FileName);

            Position_DesBricks = new List<Vector2>();
            Type_DesBricks = new List<int>();
            Position_NonDesBricks = new List<Vector2>();
            Type_NonDesBricks = new List<int>();
            Position_Monsterlv1 = new List<Vector2>();
            Position_Monsterlv2 = new List<Vector2>();

            Read_Map();
            Set_Position_Object();
        }

        private void Read_Map()
        {
            string[] temp = new string[15];
            for (int i = 0; i < 15; i++)
            {
                temp = _MyStream.ReadLine().Split(' ');
                for (int j = 0; j < 15; j++)
                {
                    Matrix[i, j] = int.Parse(temp[j]);
                }
            }
        }

        //Đọc tọa độ của người chơi, gạch và quái
        private void Set_Position_Object()
        {
            for (int i = 0; i < 15; i++)
            {
                for (int j = 0; j < 15; j++)
                {
                    if (Matrix[i, j] >= Config.MinTypeDesBrick && Matrix[i,j] <= Config.MaxTypeDesBrick)
                    {
                        Position_DesBricks.Add(new Vector2(j * 40, i * 40));
                        Type_DesBricks.Add(Matrix[i, j]);
                    }

                    if (Matrix[i, j] >= Config.MinTypeNonDesBrick && Matrix[i,j] <= Config.MaxTypeNonDesBrick)
                    {
                        Position_NonDesBricks.Add(new Vector2(j * 40, i * 40));
                        Type_NonDesBricks.Add(Matrix[i, j]);
                    }

                    if (Matrix[i, j] == Config.TypeMonsterLv1)
                    {
                        Position_Monsterlv1.Add(new Vector2(j * 40, i * 40));
                    }

                    if (Matrix[i, j] == Config.TypeMonsterLv2)
                    {
                        Position_Monsterlv2.Add(new Vector2(j * 40, i * 40));
                    }

                    if (Matrix[i, j] == Config.TypeBoss)
                    {
                        PositionBoss = new Vector2(j * 40, i * 40);
                        _Boss = true;
                    }

                    if (Matrix[i, j] == Config.TypePlayer)
                    {
                        PositionPlayer = new Vector2(j * 40, i * 40);
                    }
                }
            }
        }

    }
}
